(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
module.exports = ()();
},{}],2:[function(require,module,exports){

var tmpl = require('./tmpl.js');
var tpl = require('./tpl3.js');
var dianjing = require('./dianjing.js');
// var dj = require('./dj.js');

window.formatSize = function(e){
    if(e==0)return"0K";
    var t="BKMGTP",n=Math.log(e)/Math.LN2/10, r=Math.floor(n+.004);
    return Math.pow(1024,n-r).toPrecision(3)+t[r];
}

var REQUEST_URL = '/home/access';
var REQUEST_TYPE = 'json';
if (location.host == '' || location.host.indexOf('test') > -1) {
    REQUEST_URL = 'http://test1.baohe.mobilem.360.cn/home/access';
    REQUEST_TYPE = 'jsonp';
}

var App = {
    init : function() {
        App.initPageData();
        App.bindEvents();
    },

    getData : function() {
        var data = {
            gender : 1,
            age : 25,
            page: 1,
            _t : + new Date
        };
      
        return $.ajax({
            url :  REQUEST_URL + location.search,
            type : 'get',
            dataType : REQUEST_TYPE,
            data : data
        })
    },

    renderTpl : function(data) {
        var res = [];
        var appData = [];
        var djArray = [];

        var tagArray = window.tagArray  ||  {
          
        };

        data.forEach(function(item, index){
            item['domId'] = 'App_' + index;
            if (index%2 == 0) {
                if (item.tags) {
                    var tag = item.tags.split(';') || '';
                    tag = tag[0] || '';
                    if (tagArray[tag]) {
                        item.pic = tagArray[tag];
                    }
                }
                res.push(tmpl(tpl['tpl1'],{data : item}));
            } else {
                res.push(tmpl(tpl['tpl2'],{data : item}));
            }
            item.apps.forEach(function(info){
                if (info['loginfo']) {
                    info['loginfo']['startTime'] = + new Date;
                    info['loginfo']['index'] = index;
                    djArray.push(info['loginfo']);
                }
                if (info['id']) {
                    info['soft_id'] = info['id'];
                }
                appData.push(info);
            })
        });
        dianjing.init(appData);
        dianjing.sendLog('pv', djArray);
        qStore.app.addData(appData);
        
        res = res.join('');
        $('.wrap').append(res);
        qStore.web.checkAppStatus();
        var sd = ScrollerDetector.init();
        sd.check();
    },

    initPageData : function() {
        var data=window.ZXDATA;
        
        //console.log("data");
        var m2 = qStore.util.getQuery("m2") || qStore.util.getMID();
        var  isDefault=String(m2).charCodeAt(String(m2).length-1)%2;
        
        
      window.m2_type2 = ["靠谱理财利息超高",
            "三消益智小游戏",
            "女孩子都爱买买买",
            "北京必备APP",
            "点外卖必备",
            "备孕必备软件",
            "租房、买房都靠谱",
            "小众应用推荐",
            "美图美颜必备",
            "找兴趣，交朋友",
            "优惠省钱必备",
            "看视频发弹幕",
            "找工作、跳槽必备",
            "看小说必备",
            "神器在手，车票我有",
            "听音乐APP",
            "第一时间新闻八卦",
            "学习类高效App",
            "极速浏览器推荐",
            "旅游必备APP",
        ];
    var topic_id =["360", "347", "346", "348", "342", "353", "372", "712", "351", "357", "344", "340", "370", "355", "367", "341", "354", "362", "365", "350"];
    var titileMap = {};
    for (var i in topic_id) {
        titileMap[topic_id[i]] = m2_type2[i];
    }
  //  console.log(titileMap);
    window.pos="text_change";
    window.titileMap=titileMap;
        var list=data.data.list;
        if(!isDefault){
            window.pos+="_B";
            for(var i in list){
            list[i]['title']=titileMap[list[i]['topic_id']]||list[i]['titile'];
            }
            
        }else{
          window.pos+="_A";
        }
        qStore.util.record("","show",window.pos);

       // console.log(data)
        if (data.errno == 0) {
                var result = data.data.list;
                if (result.push && result.length > 0) {
                   // console.log("result");
                   // console.log(result);
                    App.renderTpl(result);
                  //  console.log(result);
                    // dj.init(result);
                } else {
                    console.log('数据为空');
                }
            } else {
                console.log('数据为空');
            }
        
        
    },

    bindEvents : function() {
        try {
            AndroidWebview.setScrollbackEnable(false);
        } catch(e){}

        $('.wrap').on('click', '.is-download', function(e){
            var id = $(this).closest('.js-app-item').data('apkid');
            var sid= $(this).closest('.js-app-item').data('sid');
            qStore.util.record(sid,"start",window.pos);

            if (dianjing.getDjData(id)) {
                dianjing.sendLog('ck', id);
            }
        })
    }
}

App.init();
},{"./dianjing.js":1,"./tmpl.js":3,"./tpl3.js":4}],3:[function(require,module,exports){
module.exports = (function(){
    return (function() {
        var cache = { };

        function compile(str) {
            return  "var __result__='';" +
                "__result__+='" +
                str
                    .replace(/[\r\t\n]/g, " ")
                    .split("<%").join("\t")
                    .replace(/((^|%>)[^\t]*)'/g, "$1\r")
                    .replace(/\t=(.*?)%>/g, "'+$1+'")
                    .split("\t").join("';")
                    .split("%>").join("__result__+='")
                    .split("\r").join("\\'")
                + "';return __result__";
        }

        return function(str, data) {
            if (!str) {
                throw '模板字符串为空'
            }
            if (!data) {
                throw '渲染数据为空'
            }

            // 第一层缓存，使用模板作为缓存标识
            var tplObj = cache[str];
            if (!tplObj) {
                tplObj = cache[str] = {
                    fnBody: compile(str),
                    fnCache: { }
                };
            }

            var keys = [ ], values = [ ];
            for (var k in data) {
                if (data.hasOwnProperty(k)) {
                    keys.push(k);
                    values.push(data[k]);
                }
            }

            // 第二层缓存，使用属性名作为缓存标识
            var cacheKey = keys.toString(), fn = tplObj.fnCache[cacheKey];
            if (!fn) {
                fn = tplObj.fnCache[cacheKey] = new Function(keys, tplObj.fnBody);
            }

            return fn.apply(this, values);
        };
    })();
})();
},{}],4:[function(require,module,exports){
module.exports = (function() {
		
   
    var tpl1 = [
    '        <% ',
  
    '                var tags = data.tags ? data.tags.split(\';\') : [];',
    '                tag = tags.length > 0 ? tags[0] + \'类\' : \'\';',
    '                var child = data.apps;',
    '                var tagImg = data.pic ? data.pic : false;',
    '                var domId = data.domId;',
    '                var topic_id = data.topic_id;',

    '                var index = data.domId.substring(data.domId.indexOf("_")+1);',
  
    '                var title =data.title;',

    '        %>',
    '        <li id="<%=domId%>" class="app-block block-pink <% if(tagImg) { %>pic-bg<% } %>" <% if (tagImg) { %> style="background-image: url(<%=tagImg%>)" <% } %>>',
    '            <div class="block-title">',
        '                <h2 class="f16 els"><%=title%></h2>',
    '                <p class="els">最受欢迎的<%=tag%>App >></p>',
    '            </div>',
    '            <section>',
    '                <% ',
    '                    for (var j=0; j < child.length; j++) {',
    '                        if (j > 2) { break; }',
    '                        var apkid = child[j][\'apkid\'];',
    '                        var logo_url = child[j][\'logo_url\'];',
    '                        var single_word = child[j][\'single_word\'];',
    '                        var name = child[j][\'name\'];',
    '                        var downTimes = child[j][\'download_times\'];',
    '                        var user = downTimes >= 10000 ? Math.floor(downTimes/10000) +\'万\' : downTimes;',
    '                        var fileSize = formatSize(child[j][\'size\']);',
    '                        var childDomId = domId + "_" + j;',
    '                        var sid = child[j][\'id\'];',

    '                %>',
    '                <div class="app-item js-app-item" id="<%=childDomId%>" data-sid="<%=sid %>" data-apkid="<%=apkid%>">',
    '                    <div class="app-logo">',
    '                        <img class="w100" src="http://p5.qhimg.com/d/inn/d88467b0/default_download.png" data-icon="<%=logo_url%>">',
    '                    </div>',
    '                    <div class="app-main">',
    '                        <div class="app-detail">',
    '                            <h3 class="f13 els"><%=single_word%></h3>',
    '                            <div class="app-info els f16">',
    '                                <%=name%>',
    '                            </div>',
    '                            <a href="javascript:void(0);" class="app-btn is-download btn-pos btn-pink js-app-btn">下载</a>',
    '                        </div>',
    '                        <div class="app-tail">',
    '                            <span class="app-info1"><%=user%>人在用</span>',
    '                            <span class="app-info2"><%=fileSize%></span>',
    '                        </div>',
    '                    </div>',
    '                </div>',
    '                <%',
    '                    }',
    '                %>',
    '            </section>',
    '        </li>'
    ].join("");

    var tpl2 = [
    '        <% ',
  
    '                var tags = data.tags ? data.tags.split(\';\') : [];',
    '                tag = tags.length > 0 ? tags[0] + \'类\' : \'\';',
    '                var child = data.apps;',
    '                var tagImg = data.pic ? data.pic : false;',
    '                var domId = data.domId;',
    '                var index = data.domId.substring(data.domId.indexOf("_")+1);',
  
    '                var title =data.title;',
    '        %>',
    '        <li id="<%=domId%>" class="app-scroll">',
    '            <div class="block-title">',
    '                <h2 class="f16 els"><%=title%></h2>',
    '                <p class="els">最受欢迎的<%=tag%>App >></p>',
    '            </div>',
    '            <section class="scroll-wrap">',
    '                <ul>',
    '                    <% ',
    '                        for (var j=0; j < child.length; j++) {',
    '                            var apkid = child[j][\'apkid\'];',
    '                            var logo_url = child[j][\'logo_url\'];',
    '                            var single_word = child[j][\'single_word\'];',
    '                            var name = child[j][\'name\'];',
    '                            var downTimes = child[j][\'download_times\'];',
    '                            var user = downTimes >= 10000 ? downTimes/10000 +\'万\' : downTimes;',
    '                            var fileSize = formatSize(child[j][\'size\']);',
    '                            var childDomId = domId + "_" + j;',
    '                        var sid = child[j][\'id\'];',
    '                    %>',
    '                    <li class="app-scroll-wrap app-item js-app-item" id="<%=childDomId%>"  data-sid="<%= sid%>" data-apkid="<%=apkid%>">',
    '                        <div class="app-logo">',
    '                            <img class="w100" src="http://p5.qhimg.com/d/inn/d88467b0/default_download.png" data-icon="<%=logo_url%>">',
    '                        </div>',
    '                        <h3 class="els f14"><%=name%></h3>',
    '                        <p class="els f12"><%=single_word%></p>',
    '                        <a href="javascript:void(0);" class="app-btn btn-pink is-download js-app-btn">下载</a>',
    '                    </li>',
    '                    <%',
    '                        }',
    '                    %>',
    '                </ul>',
    '            </section>',
    '        </li>'
    ].join("");

    return {
        tpl1 : tpl1,
        tpl2 : tpl2
    }
}());
},{}]},{},[2]);